/**
 * Created by wuye on 2017/12/13.
 */

// $(function () {
//获得后台数据
// var month_top;             //月销售前五的小说
// var soft_data;                //软板块销售数据
// var net_data;               //网板块销售数据
// var mobile_data;           //移动板块销售数据
// $.ajax({
//     url: "/JavaEchartsJson/servlet/JavaEchartsJson",
//     async: false,
//     type: 'GET',
//     dataType: 'json',
//     timeout: 1000,
//     cache: false,
//     success: function (data) {
//         //成功时执行的回调方法
//         category_data = data.categor;
//         soft_data = data.soft_data;
//         net_data = data.net_data;
//         mobile_data = data.mobile_data;
//     },
//     error: erryFunction                 //错误时执行方法
// });
//
// function erryFunction() {
//     alert("数据没返回成功！");
// };


function category(data) {
    if (data) {
        var myChart4 = echarts.init(document.getElementById('main4'));
        var newData = [];
        var newDataName = [];

        for (var one in data) {
            newData.push({
                name: one,
                value: data[one]
            })

            newDataName.push(one)
        }




        var option4 = {
            title: {
                text: '蝉阅读书籍统计',
                subtext: '分类统计',
                left: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                // orient: 'vertical',
                // top: 'middle',
                bottom: 10,
                left: 'center',
                data: newDataName
            },
            series: [{
                type: 'pie',
                radius: '65%',
                center: ['50%', '50%'],
                selectedMode: 'single',
                data: newData,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]
        };




        myChart4.setOption(option4);
    }
}



function users(data) {
    // 统计用户
    if (!data) {
        return false
    }

    var myChart2 = echarts.init(document.getElementById('main2'));

    var option2 = {
        title: {
            text: '用户统计',
            subtext: '男生&女生',
            left: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            // orient: 'vertical',
            // top: 'middle',
            bottom: 10,
            left: 'center',
            data: ['男生', '女生']
        },
        series: [{
            type: 'pie',
            radius: '65%',
            center: ['50%', '50%'],
            selectedMode: 'single',
            data: [{
                    value: data.nan,
                    name: '男生'
                },
                {
                    value: data.nv,
                    name: '女生'
                },
            ],
            itemStyle: {
                emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }]
    };


    myChart2.setOption(option2);


}



function top5(data) {
    // 前5

    var myChart = echarts.init(document.getElementById('main'));

    var top5Name = []
    var top5Val = []

    var _DATA = data.tdBookListSellTop
    for (var i in _DATA) {
        top5Name.push(_DATA[i].bookname)
        top5Val.push(_DATA[i].sell)
    }

    var option1 = {
        title: {
            text: '蝉阅读小说月销售TOP5'
        },
        color: ['rgb(25, 183, 207)'],
        tooltip: {},
        legend: {
            data: ['销量']
        },
        xAxis: {
            data: top5Name
        },
        yAxis: {},
        series: [{
            name: '销量',
            type: 'bar',
            data: top5Val
        }]
    };


    myChart.setOption(option1);
}


function topAll(data) {

    var myChart3 = echarts.init(document.getElementById('main3'));

    var top5Name = []
    var top5Val = []

    var _DATA = data.tdBookListSellTop
    for (var i in _DATA) {
        top5Name.push(_DATA[i].bookname)
        top5Val.push(_DATA[i].sell)
    }

    var option3 = {
        title: {
            text: '蝉阅读小说累计销售TOP5'
        },
        color: ['rgb(25, 183, 207)'],
        tooltip: {},
        legend: {
            data: ['销量']
        },
        xAxis: {
            data: top5Name
        },
        yAxis: {},
        series: [{
            name: '销量',
            type: 'bar',
            data: top5Val
        }]
    };


    myChart3.setOption(option3);


}

$(function () {
    function getDataFromAPI() {
        $.ajax({
            url: baseURL + 'sys/statis/findBookTop',
            dataType: 'json',
            data: {

            },
            success: function (result) {
                console.log(result)
                category(result.result.category)
                users(result.result)
                top5(result.result)
                topAll(result.result)
            },
            error: function (result) {
                alert('抱歉,数据加载失败:' + result.msg)
            }
        })
    };



    getDataFromAPI()
});


// 使用刚指定的配置项和数据显示图表。
// })